package cn.iborder.handler;

import java.util.Map;

import javax.enterprise.inject.New;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;
import com.jfinal.kit.HandlerKit;
import com.jfinal.kit.StrKit;

public class UrlInterceptHandler extends Handler {
	
	private Map suffix;
	
	private UrlInterceptHandler() {}
	
	/**
	 * 过滤禁止访问的后缀
	 * @param suffix 存放键值对:后缀名-拦截开关(true:拦截 , false:通过)
	 */
	public UrlInterceptHandler(Map<String, Boolean> suffix) {
		if (suffix == null) {
			throw new IllegalArgumentException("suffix can not be blank.");
		}
		this.suffix = suffix;
	}
	
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		String tmp = "";
		int index = target.lastIndexOf('/');
		if (index != -1) {
			tmp = target.substring(index+1, target.length());
		}
		index = tmp.indexOf('.');
		if (index != -1) {
			tmp = tmp.substring(index+1, tmp.length());
		}
		if (suffix.containsKey(tmp)) {
			if ((boolean) suffix.get(tmp)) {
				HandlerKit.renderError404(request, response, isHandled);
				return ;
			}
		}
		next.handle(target, request, response, isHandled);
	}
}
